package com.controller;

import com.dao.MajorDetilDao;
import com.dao.StudentInfomationDetialDao;
import com.dao.impl.CourseDetailDaoImp;
import com.dao.impl.MajorDetilDaoImp;
import com.dao.impl.StudentInformationDetilDaoImp;
import com.entiy.CourseinformationtableEntity;
import com.entiy.LogintableEntity;
import com.entiy.MajortableEntity;
import com.entiy.StudenttableEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class StudentDetailController extends ActionSupport {
    StudentInfomationDetialDao studentInfomationDetialDao;
    private StudenttableEntity studenttableEntity;
    private CourseinformationtableEntity courseinformationtableEntity;
    private MajortableEntity majortableEntity;

    public StudentInfomationDetialDao getStudentInfomationDetialDao() {
        return studentInfomationDetialDao;
    }

    public void setStudentInfomationDetialDao(StudentInfomationDetialDao studentInfomationDetialDao) {
        this.studentInfomationDetialDao = studentInfomationDetialDao;
    }

    public StudenttableEntity getStudenttableEntity() {
        return studenttableEntity;
    }

    public void setStudenttableEntity(StudenttableEntity studenttableEntity) {
        this.studenttableEntity = studenttableEntity;
    }

    public CourseinformationtableEntity getCourseinformationtableEntity() {
        return courseinformationtableEntity;
    }

    public void setCourseinformationtableEntity(CourseinformationtableEntity courseinformationtableEntity) {
        this.courseinformationtableEntity = courseinformationtableEntity;
    }

    public MajortableEntity getMajortableEntity() {
        return majortableEntity;
    }

    public void setMajortableEntity(MajortableEntity majortableEntity) {
        this.majortableEntity = majortableEntity;
    }
    public String execute() throws Exception {
        Map session=(Map) ActionContext.getContext().getSession();
        LogintableEntity user=(LogintableEntity)session.get("user");
        studentInfomationDetialDao=new StudentInformationDetilDaoImp(){};
        StudenttableEntity xs=studentInfomationDetialDao.getInfo(user.getlogin());
        Map request=(Map)ActionContext.getContext().get("request");
        request.put("xs", xs);
        return "success";
    }
    public String updateXsInfo() throws Exception{
        //获取当前用户对象
        Map session=(Map)ActionContext.getContext().getSession();
        LogintableEntity user=(LogintableEntity) session.get("user");
        studentInfomationDetialDao=new StudentInformationDetilDaoImp();
        MajorDetilDao zyDao=new MajorDetilDaoImp();
        List zys=zyDao.getAll();
        //得到当前学生的信息
        StudenttableEntity xs=studentInfomationDetialDao.getInfo(user.getlogin());
        Map request=(Map)ActionContext.getContext().get("request");
        request.put("zys", zys);
        request.put("xs", xs);
        return SUCCESS;
    }
    public String updateXs() throws Exception{
        studentInfomationDetialDao=new StudentInformationDetilDaoImp();
        MajorDetilDao zyDao=new MajorDetilDaoImp();
        StudenttableEntity stu=new StudenttableEntity();
        stu.setNumber(studenttableEntity.getNumber());
        stu.setName(studenttableEntity.getName());
        stu.setBirthday(studenttableEntity.getBirthday());
        stu.setSex(studenttableEntity.getSex());
        stu.setRemarks(studenttableEntity.getRemarks());
        stu.setTotalcredits(studenttableEntity.getTotalcredits());

        MajortableEntity zy=zyDao.getInfo(majortableEntity.getId());
        stu.setMajorDetail(zy);
        //由于没有修改学生对应的选修的课程，所以直接取出不用改变
        //Hibernate 级联到第三张表，所以要设置，如果不设置，会认为设置为空，会把连接表中有关内容删除
        Set list=studentInfomationDetialDao.getInfo(studenttableEntity.getNumber()).getCourse();
        //设置学生对应多项课程的 Set
        stu.setCourse(list);
        //修改
        studentInfomationDetialDao.update(stu);
        return SUCCESS;
    }

    public String getXsKcs() throws Exception{
        Map session=(Map)ActionContext.getContext().getSession();
        LogintableEntity user=(LogintableEntity) session.get("user");
        String studentNo=user.getlogin();
        //得到当前学生的信息
        StudenttableEntity xsb=new StudentInformationDetilDaoImp().getInfo(studentNo);
        //取出选修的课程 Set
        Set list=xsb.getCourse();
        Map request=(Map)ActionContext.getContext().get("request");
        //保存
        request.put("list",list);
        return SUCCESS;
    }

    public String deleteKc() throws Exception{
        Map session=(Map)ActionContext.getContext().getSession();
        String xh=((LogintableEntity)session.get("user")).getlogin();
        studentInfomationDetialDao=new StudentInformationDetilDaoImp();
        StudenttableEntity xs2=studentInfomationDetialDao.getInfo(xh);
        Set list=xs2.getCourse();
        Iterator iter=list.iterator();
        while(iter.hasNext()){
            CourseinformationtableEntity kc2=(CourseinformationtableEntity) iter.next();
            //如果遍历到退选的课程的课程号就从list中删除
            if(kc2.getCoursenumber().equals(courseinformationtableEntity.getCoursenumber())){
                iter.remove();
            }
        }
        //设置课程的 Set
        xs2.setCourse(list);
        studentInfomationDetialDao.update(xs2);
        return SUCCESS;
    }
    public String selectKc() throws Exception{
        Map session=(Map)ActionContext.getContext().getSession();
        String xh=((LogintableEntity)session.get("user")).getlogin();
        studentInfomationDetialDao=new StudentInformationDetilDaoImp();
        StudenttableEntity xs3=studentInfomationDetialDao.getInfo(xh);
        Set list=xs3.getCourse();
        Iterator iter=list.iterator();
        //选修课程时先遍历已经选的课程，如果在已经选修的课程中找到就返回 ERROR
        while(iter.hasNext()){
            CourseinformationtableEntity kc3=(CourseinformationtableEntity) iter.next();
            if(kc3.getCoursenumber().equals(courseinformationtableEntity.getCoursenumber())){
                return ERROR;
            }
        }
        //如果没找到，就添加到集合中
        list.add(new CourseDetailDaoImp().getInfo(courseinformationtableEntity.getCoursenumber()));
        xs3.setCourse(list);
        studentInfomationDetialDao.update(xs3);
        return SUCCESS;
    }
}
